<?php

namespace App\V3\V3_2\Controllers;

use App\V3\V3_2\Models\AnnounceModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

class AnnounceController extends BaseController
{
    /**
     * 新建公告
     * @param Request $request
     * AUTHOR  : Sun Wu
     * DateTime: 2022/6/14 18:15
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request)
    {
        $data = $request->all();
        if(empty($data['title'])) {
            return $this->fail('', '请填写标题');
        }

        $id = AnnounceModel::insertGetId([
            'title' => $request->title,
            'content' => empty($data['content'])?'':$data['content'],
            'created_at' => time()
        ]);

        $this->log('新建公告', $data);
        return $this->success($id);
    }

    /**
     * 编辑
     * @param Request $request
     * AUTHOR  : Sun Wu
     * DateTime: 2022/6/14 18:21
     * @return \Illuminate\Http\JsonResponse
     */
    public function edit(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'id' =>'required|integer|min:1',//页面大小
            'title' =>'required',
        ],[
            'id.required' => '公告主键id缺失',
            'id.integer' => '公告id类型错误',
            'title.required' => '公告标题缺失',
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        $this->log('新建公告', $data);

        AnnounceModel::where('id', $data['id'])
            ->update([
                'title' => $data['title'],
                'content' => empty($data['content'])?'':$data['content'],
            ]);

        return $this->success();
    }

    /**
     * 删除
     * @param Request $request
     * AUTHOR  : Sun Wu
     * DateTime: 2022/6/14 18:32
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'id' =>'required|integer|min:1',//页面大小
        ],[
            'id.required' => '公告主键id缺失',
            'id.integer' => '公告id类型错误',
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        $this->log('删除公告', $data);

        AnnounceModel::where('id', $data['id'])->update(['delete_flag' => 1]);
        return $this->success();
    }

    /**
     * 列表
     * @param Request $request
     * AUTHOR  : Sun Wu
     * DateTime: 2022/6/14 18:39
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'pageSize' =>'required|integer|min:1',//页面大小
            'currentPage' =>'required|integer|min:1',//页面大小
        ],[
            'pageSize.required' => '条数缺失',
            'pageSize.integer' => '条数数据类型错误',
            'currentPage.required' => '页码缺失',
            'currentPage.integer' => '页码缺失数据类型错误',
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        return $this->success(
            AnnounceModel::where('delete_flag', 0)
                ->orderBy('is_publish', 'desc')
                ->orderBy('id', 'desc')
                ->paginate($data['pageSize'],['*'], '',$data['currentPage'])->toArray()
        );
    }

    public function publish(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'id' =>'required|integer|min:1',//页面大小
        ],[
            'id.required' => '公告主键id缺失',
            'id.integer' => '公告id类型错误',
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        $publishInfo = AnnounceModel::where('id', '!=', $data['id'])
            ->where('is_publish', 1)
            ->first();

        if(!empty($publishInfo)) {
            AnnounceModel::where('id', $publishInfo['id'])
                ->update(['is_publish' => 0]);
        }

        AnnounceModel::where('id', $data['id'])
            ->update(['is_publish' => 1]);

        $this->log('发布公告', $data);
        return $this->success();
    }
}
